package com.woniuxy.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.dto.system.DeptDTO;
import com.woniuxy.entity.po.system.Dept;
import com.woniuxy.entity.po.system.User;
import com.woniuxy.mapper.DeptMapper;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.service.DeptService;
import com.woniuxy.util.PageHelperUtil;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class DeptServiceImpl implements DeptService {
    private DeptMapper deptMapper;
    private UserMapper userMapper;
    private PageHelperUtil pageHelperUtil;
    /**
     * 查询所有的dept数据，带到页面回显（分页回显）
     * 增删改查中的查
     * @return
     */
    @Override
    public ResponseData<?> queryAll(PageParam<DeptDTO> pageParam) {
        PageInfo<DeptDTO> pageInfo = pageHelperUtil.getPageInfo(pageParam, deptMapper::queryAll, DeptDTO::new);
        return ResponseData.ok(pageInfo);
    }

    @Override
    public ResponseData<?> queryIdInfo(Long id) {
        Dept dept = deptMapper.queryIdInfo(id);
        DeptDTO deptDTO = new DeptDTO(dept);
        return ResponseData.ok(deptDTO);
    }

    @Override
    public Integer deleteDept(Long id) {
        List<User> users = userMapper.queryDeptIdInfo(id);
        if (users.size() == 0){
            Integer integer = deptMapper.deleteDept(id);
            return integer;
        }else {return 0;}

    }

    @Override
    public void batchDeleteDept(List<Long> ids) {
        for (Long id : ids) {
            if (userMapper.queryDeptIdInfo(id) == null){
                deptMapper.deleteDept(id);
            }
        }
//        deptMapper.batchDeleteDept(ids);
    }

    @Override
    public void addDept(DeptDTO deptDTO) {
        Dept dept = new Dept(deptDTO);
        deptMapper.addDept(dept);
    }

    @Override
    public void updateDept(DeptDTO deptDTO) {
//        deptDTO.setId(String.valueOf(IdEncryptor.decrypt(deptDTO.getId())));
        Dept dept = new Dept(deptDTO);
        deptMapper.updateDept(dept);
    }

    @Override
    public List<Dept> queryDept() {
        return deptMapper.queryDept();
    }

}
